/**
 medio_seguro.java is part of Marioneta.

  Marioneta is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License.

  Marioneta is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
  
  @author martin
   Martin Edmundo Barriga Orozco, 2008
   martin.barriga@gulmore.org
  
  medio_seguro: Clase prototipo que pretende asegurar todo el Strem de flujo de
  una comunicacion, con esta los datos en una session TCP podran estar seguros. 
 */

package escritorioremoto;

import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import java.io.OutputStream;
import java.io.InputStream;

import java.io.File;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
//import org.bouncycastle.crypto.StreamBlockCipher;
//import org.bouncycastle.crypto.BlockCipher;
//import org.bouncycastle.crypto.StreamCipher;
//import org.bouncycastle.crypto.modes.CFBBlockCipher;
//import org.bouncycastle.crypto.engines.AESLightEngine;

public class medio_seguro {
    private cifrador obj_cifrador;
    private CipherInputStream flujoIn;
    private CipherOutputStream flujoOut;
    
    public medio_seguro( String tipo ){
        this.obj_cifrador = new cifrador( tipo );
    }
    
    public medio_seguro(String tipo, SecretKey key ){
        this.obj_cifrador = new cifrador( tipo, key );
    }
    
    public medio_seguro( cifrador cifrador_ ){
        this.obj_cifrador = cifrador_;
    }
    
    public void setLlave( SecretKey key ){
        obj_cifrador.setLlave( key );
    }
    
    public SecretKey getLlave(){
        return obj_cifrador.getLlave();
    }
    
    private OutputStream getFlujoOS( OutputStream os ){
        OutputStream out = new CipherOutputStream(os, obj_cifrador.getCipher() );
        return out;
    }
    
    private InputStream getFlujoIS( InputStream is ){
        InputStream in = new CipherInputStream(is, obj_cifrador.getCipher() );
        return in;
    }
    
    //Antes de obtener un flujo  debemos colocar el modo de operacion        
    private void setModeOp( boolean mode ){
        this.obj_cifrador.setCipherVal(mode);
    }
    
    public boolean ctl_fichero(String origen, String destino, boolean encryt){
        //Abrir archivo para lectura
        File miFich = new File( origen );
        if ( miFich.exists() ){
            try{
               int  cp;
               //Abrir archivo para lectura y escritura
               FileInputStream FIS = new FileInputStream( origen );
               DataInputStream DIS = new DataInputStream( FIS );
               
               FileOutputStream FOS = new FileOutputStream( destino );
               DataOutputStream DOS = new DataOutputStream( FOS );
               
               //Indicar el modo de operacion con el flujo
               this.setModeOp( encryt );
               InputStream IS = this.getFlujoIS( DIS );
               
               //Comenzamos a copiar byte por byte
               while ( ( cp = IS.read() ) != -1 )
                   DOS.write( cp );
               
               //Cerramos flujos
               DOS.close();
               DIS.close();
               return true;
            }
            catch( FileNotFoundException e ){
               return false;
            }
            catch( IOException e ){
                System.out.println("Fin del archivo");
                return false;
            }
        }
        else{
            System.out.println("No existe el archivo :( ");
            return false;
        }
        
    }
    
    public static void main(String[] args) {
        medio_seguro obj = new medio_seguro("DES");
        obj.ctl_fichero("imagen.gif", "imagen.gpg", true );
 
        medio_seguro obj2 = new medio_seguro("DES", obj.getLlave() );
        obj2.ctl_fichero("imagen.gpg", "NuevaImagen.gif", false );

        //AESLightEngine blockCipher = new AESLightEngine();
        //CFBBlockCipher cfbCipher = new CFBBlockCipher(blockCipher, 8);
        //StreamCipher streamCipher = new StreamBlockCipher(cfbCipher);
    }
    
}